﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Demo_HHT_EARTH
{
    public partial class DLflightInfo : Form
    {
        SQLiteConnection cn2 = new SQLiteConnection();
        DataSet dsGetflight = new DataSet();
        DataSet dataEmp = new DataSet();
        DataSet dataEq = new DataSet();
        string flight_no, strconinsert = string.Empty;
        public DLflightInfo(DataSet dsgf ,DataSet dsem, DataSet dseq,string flight_no )
        {
            InitializeComponent();

            this.dsGetflight = dsgf;
            this.dataEmp = dsem;
            this.dataEq = dseq;
            this.flight_no = flight_no;
            if (dsGetflight == null || dataEmp == null || dataEq == null) //(DatafldInfo==null || data_emp == null|| data_eq == null)
            {
                MessageBox.Show("Download Data Error");
            }
            else
            {



                ClearTable();
                InsertDataFlightInfo(dsGetflight);
               
            }

        }

        private void ClearTable()
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }
            try
            {
                strconinsert = "DELETE from t_earth_flight";


                SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
                cn2.Open();
                command.Connection = cn2;

                command.ExecuteNonQuery();
                cn2.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("Command Error");
            }
           
        }
        DataTable dt = new DataTable();
        Guid flig_id = new Guid();
        private void InsertDataFlightInfo(DataSet DataEmp)
        {
            DataSet datafldInfo = new DataSet();
            datafldInfo = DataEmp;
            if (datafldInfo.Tables[0].Rows.Count > 0)
            {
                try
                {
                    cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
                }
                catch (Exception)
                {

                    MessageBox.Show("notconect SQLite");
                }
                dt = new DataTable();
                dt = datafldInfo.Tables[0];
                if (dt == null)
                {
                    MessageBox.Show("no data");
                }
                else
                {

                    //progressBar1.Maximum = dt.Rows.Count;

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string datenow = String.Format("{0:M/d/yyyy}", DateTime.Now);
                        flig_id = Guid.NewGuid();
                        strconinsert = "INSERT INTO [t_earth_flight] (flight_id,flight_date,fid,flight_number,carrier,carrier_name,scheduled_time_arrival,scheduled_time_departure,bay,routing,flight_status,display_flight_date,display_sta_time,display_std_time,display_flight_number,create_date,create_by) VALUES('"
                                    + flig_id.ToString()
                            + "','" + dt.Rows[i]["flight_date"].ToString()
                            + "','" + dt.Rows[i]["fid"].ToString()
                            + "','" + dt.Rows[i]["flight_number"].ToString()
                            + "','" + dt.Rows[i]["carrier"].ToString()
                            + "','" + dt.Rows[i]["carrier_name"].ToString()
                            + "','" + dt.Rows[i]["scheduled_time_arrival"].ToString()
                            + "','" + dt.Rows[i]["scheduled_time_departure"].ToString()
                            + "','" + dt.Rows[i]["bay"].ToString()
                            + "','" + dt.Rows[i]["routing"].ToString()
                            + "','" + dt.Rows[i]["flight_status"].ToString()
                            + "','" + dt.Rows[i]["display_flight_date"].ToString()
                            + "','" + dt.Rows[i]["display_sta_time"].ToString()
                            + "','" + dt.Rows[i]["display_std_time"].ToString()
                            + "','" + dt.Rows[i]["display_flight_number"].ToString()
                            + "','" + datenow
                            + "','" + Demo_HHT_EARTH.GobalVariableEARTH.UserID
                            + "')";


                        SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
                        cn2.Open();
                        command.Connection = cn2;

                        command.ExecuteNonQuery();
                        cn2.Close();
                       
                    }
                    DisplayFlightInfo();
                }

            }
            else
            {
                label1.Visible = true;
                label1.Text = "Can not Getflight Information";
            }
        }

        private void DisplayFlightInfo()
        {
            label8.Text = dt.Rows[0]["display_flight_date"].ToString();
            label9.Text = dt.Rows[0]["display_flight_number"].ToString();
            if (Demo_HHT_EARTH.GobalVariableEARTH.flight_status.Equals("Arrival"))
            {
                label10.Text = "";
                label11.Text = dt.Rows[0]["display_sta_time"].ToString();
            }
            else if (Demo_HHT_EARTH.GobalVariableEARTH.flight_status.Equals("Departure"))//Departure
	        {
                label11.Text = "";
                label10.Text = dt.Rows[0]["display_std_time"].ToString(); 
	        }
            else
            {
                label11.Text = dt.Rows[0]["display_sta_time"].ToString();
                label10.Text = dt.Rows[0]["display_std_time"].ToString(); 
            }


            label12.Text = dt.Rows[0]["routing"].ToString();
            label13.Text = dt.Rows[0]["bay"].ToString();
            Demo_HHT_EARTH.DataGetflightInfo.Gufld_id = flig_id.ToString();
        }
        private void panel1_GotFocus(object sender, EventArgs e)
        {

        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
            Demo_HHT_EARTH.DLflight dl = new DLflight();
            dl.Show();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Demo_HHT_EARTH.DLflight2 dl2 = new DLflight2(dsGetflight, dataEmp, dataEq, flight_no); 
            dl2.Show();
        }
    }
}